package cn.iocoder.yudao.module.member.service.jobmajor;

import cn.iocoder.yudao.module.member.controller.admin.jobmajor.vo.JobMajorPageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.jobmajor.vo.JobMajorSaveReqVO;
import cn.iocoder.yudao.module.member.dal.mysql.jobmajor.JobMajorMapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.springframework.stereotype.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

import cn.iocoder.yudao.module.member.dal.dataobject.jobmajor.JobMajorDO;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.module.member.enums.ErrorCodeConstants.*;

import javax.annotation.Resource;

import static cn.iocoder.yudao.framework.common.exception.util.ServiceExceptionUtil.exception;

/**
 * 岗位专业中间 Service 实现类
 *
 * @author Alan
 */
@Service
@Validated
public class JobMajorServiceImpl implements JobMajorService {

    @Resource
    private JobMajorMapper jobMajorMapper;

    @Override
    public Long createJobMajor(JobMajorSaveReqVO createReqVO) {
        // 插入
        JobMajorDO jobMajor = BeanUtils.toBean(createReqVO, JobMajorDO.class);
        jobMajorMapper.insert(jobMajor);
        // 返回
        return jobMajor.getId();
    }

    @Override
    public void updateJobMajor(JobMajorSaveReqVO updateReqVO) {
        // 校验存在
        validateJobMajorExists(updateReqVO.getId());
        // 更新
        JobMajorDO updateObj = BeanUtils.toBean(updateReqVO, JobMajorDO.class);
        jobMajorMapper.updateById(updateObj);
    }

    @Override
    public void deleteJobMajor(Long id) {
        // 校验存在
        validateJobMajorExists(id);
        // 删除
        jobMajorMapper.deleteById(id);
    }
    @Override
    public void deleteJobIdMajor(Long jobId) {

        // 删除
        jobMajorMapper.delete(new LambdaQueryWrapper<JobMajorDO>().eq(JobMajorDO::getJobId,jobId));
    }

    private void validateJobMajorExists(Long id) {
        if (jobMajorMapper.selectById(id) == null) {
            throw exception(JOB_MAJOR_NOT_EXISTS);
        }
    }

    @Override
    public JobMajorDO getJobMajor(Long id) {
        return jobMajorMapper.selectById(id);
    }

    @Override
    public PageResult<JobMajorDO> getJobMajorPage(JobMajorPageReqVO pageReqVO) {
        return jobMajorMapper.selectPage(pageReqVO);
    }

    @Override
    public Set<Long> getJobMajorList(Long id) {
        List<JobMajorDO> jobMajorDOS = jobMajorMapper.selectList(new LambdaQueryWrapper<JobMajorDO>().eq(JobMajorDO::getJobId, id));
        Set<Long> collect = jobMajorDOS.stream().map(JobMajorDO::getMajorId).collect(Collectors.toSet());
        return collect;
    }
}